package com.bz.zhrc.task;

import com.bz.zhrc.service.PushService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
@EnableScheduling
public class PushTask {

    private final PushService pushService;

    //定时任务
    @Scheduled(cron = "0 0 0 * * ? ")
    public void push(){
        log.info("------推送任务开始------");
        try {
            /*
            执行PushService中的getTree方法，并遍历方法的结果。以只读的形式遍历方法
             */
            pushService.getTree().forEach(pushService::getAndPushDevice);
        }catch (Exception e){
            log.info("推送任务异常：{}",e.getMessage());
            e.printStackTrace();

        }
//        log.info("------推送任务结束------");
    }
}
